package com.entfrm.biz.system.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.entfrm.biz.system.entity.Role;
import com.entfrm.biz.system.entity.User;
import com.entfrm.biz.system.entity.UserRole;
import com.entfrm.biz.system.service.RoleService;
import com.entfrm.biz.system.service.UserRoleService;
import com.entfrm.core.base.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <h3>entfrm</h3>
 * <p>角色处理类</p>
 *
 * @author : kyc
 * @date : 2020-08-24 10:47
 **/
@Component
public class RoleManager {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    public R changeStatus(Role role) {
        List<UserRole> list = userRoleService.list(new QueryWrapper<UserRole>().lambda().eq(UserRole::getRoleId, role.getId()));
        if (list != null && !list.isEmpty()) {
            return R.error("角色有绑定用户无法禁用!");
        }else {
            roleService.update(new UpdateWrapper<Role>().eq("id", role.getId()).set("status", role.getStatus()));
            return R.ok();
        }
    }
}
